package com.chinasoft.service.impl;

import com.chinasoft.mapper.BrandMapper;
import com.chinasoft.pojo.Brand;
import com.chinasoft.pojo.PageItem;
import com.chinasoft.service.BrandService;
import com.chinasoft.utils.MybatisUtils;
import lombok.Setter;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 中软zhang
 * @create 2023-08-07 13:50
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper mapper;

    @Override
    public List<Brand> getAllBrand() {

        // 执行 selectAll sql语句
        List<Brand> brandList = mapper.selectAll();
        return brandList;
    }

    @Override
    public Brand getOneBrand(Integer id) {
        // 执行 selectAll sql语句
        Brand brand = mapper.selectById(id);
        return brand;
    }

    @Override
    public boolean insertBrand(Brand brand) {

        // 执行 insert
        int ret = mapper.insert(brand);

        //  判断插入是否成功
        if (ret != 1) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean updateBrand(Brand brand) {

        // 执行 insert
        int ret = mapper.update(brand);

        //  判断插入是否成功
        if (ret != 1) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean deleteSome(int[] ids) {

        // 执行 insert
        int ret = mapper.deleteSome(ids);

        //  判断插入是否成功
        if (ret != ids.length) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public PageItem<Brand> queryPage(Integer current, Integer size) {

        // 计算 第几条开始的数据
        Integer from = (current - 1) * size;
        List<Brand> brandList = mapper.selectPage(from, size);
        // 查询 总共有多少条
        Integer total = mapper.selectCount();
        PageItem<Brand> pi = new PageItem<Brand>(total, brandList);

        // 返回对象
        return pi;
    }

    @Override
    public PageItem<Brand> queryConditionPage(Brand brand, Integer current, Integer size) {

        // 计算 第几条开始的数据
        Integer from = (current - 1) * size;
        List<Brand> brandList = mapper.selectCondPage(brand, from, size);
        // 查询 总共有多少条
        Integer total = mapper.selectCondCount(brand);
        PageItem<Brand> pi = new PageItem<Brand>(total, brandList);

        // 返回对象
        return pi;
    }
}
